
#ifndef FREEADDRESSESBLOCK_H_
#define FREEADDRESSESBLOCK_H_

#include <unistd.h>
#include <vector>
#include "DiskUtils.h"

using namespace std;


class FreeAddressesBlock {

private:
	int offset;
	int blockSize;
	//TODO change to int*
	vector<int> freeAddresses;
	int nextFreeAddressesBlockIndex;
	//points on the next free place in the array
	int currentFreeSlotForNewAddress;
	//points on the next free address
	int freeAddressesPointer;

//	int givenAddressesCounter;

	int fileDescriptor;

public:
	FreeAddressesBlock(int blockOffset, int sizeOfBlock, int fileSysDescriptor);
	virtual ~FreeAddressesBlock();
	/* returns the free address on success, -1 otherwise*/
	int getNextFreeAddress();
	/* returns 0 on success, -1 otherwise */
	int addFreeAddress(int newFreeAddress);
	void removeNextFreeAddress();
	/* returns the next block of free blocks addresses if there exists one
	  -1 otherwise */
	int getNextFreeAddressesBlock();
	/* returns true if there is still place for another address, false otherwise */
	void setNextFreeAddressesBlock(int nextAddress);
	bool hasMoreSpace();
	/* returns true if the field of the next free addresses block is set, false otherwise */
	bool hasPointerToNextBlock();
	void loadFromPhysicalDisk();
	void writeToPhysicalDisk();
	/* copies all the addresses from otherBlock and resets it*/
	void copyToSelfAndRemoveOther(FreeAddressesBlock& otherBlock);
	void resetBlock();
	void setOffset(int newOffset);

	void printAddresses();


};

#endif /* FREEADDRESSESBLOCK_H_ */
